clear all;
addpath('liblinear-1.7/matlab');  % add LIBLINEAR to the path
load('zoom_Data.mat', 'flag1');
l = flag1(:,2);
%**************************************************************************
%Input parameters

% Scales conversions
scale_conversion_factor_tracking = 1.5;
lecturer_tracking_scale_factor = 640/2560;
saliency_tracking_scale_factor = 640/1920;
board_scale_factor = 640/1920;

num_points = 50000;

% Kalman smoothing parameters
ground_truth_smooth_factor = 150;
final_output_smooth_factor = 250;

% Parameters for panning label prediction
frame = 500; % Sliding window size
numsteps = 5;
step = round(frame/numsteps); % Step size.
derivative_range = 30:30:150;

% Parameters for trajectory regression
frame_offset = 15900;
sample_step = frame_offset/10;

% SVM Parameters
log2c = 0.95;
log2g = 1.80;

% Panning noise removal parameters
pan_label_trough_width = 200;
pan_label_spike_width = 50;

% Rectification look ahead parameter
rectify_future_max = 500;


load lecturer.dat;
load saliency.dat;
load boards.dat;

%**************************************************************************
%prepare features
%calculate board center in x-direction
board_center_x = (boards(:,1) + boards(:,2))/2;
board_center_x = board_center_x*board_scale_factor;

% Prepare features first

% Scale vectors
%lecuturer feature adjustment
lecturer = lecturer * lecturer_tracking_scale_factor;

%saliency feature adjustment
saliency = saliency *  saliency_tracking_scale_factor;

X_zoom = PrepareFeatures(1, num_points, frame_offset, sample_step, lecturer, saliency, board_center_x);
X_zoom_splice(:,:) = X_zoom(:,1:size(flag1,1)); 
X_zoom_splice = X_zoom_splice';


% Cross validation ranges
train_range = [1:4000; 1:3000 4001:5000; 1:2000 3001:5000; 1:1000 2001:5000; 1001:5000];
test_range = [4001:5000; 3001:4000; 2001:3000; 1001:2000; 1:1000];

% Code testing ranges
% train_range = [1001:5000];
% test_range = [1:1000];

for seq_range=1:size(train_range,1)
    
training_seq = train_range(seq_range,:);
testing_seq = test_range(seq_range,:);

% Step 1: Train the SVM to assign a panning label
cmd = ['-t 2 -c ', num2str(2^log2c), ' -g ', num2str(2^log2g)];
model =  svmtrain(l(training_seq), X_zoom_splice(training_seq,:) ,cmd);
[predicted_panning_label, accuracy, decision_values] = svmpredict(l(testing_seq), X_zoom_splice(testing_seq,:), model);
[precision, recall, accuracy] = computePrecisionRecall(predicted_panning_label, l(testing_seq));
end
